package org.exez.modules.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.exez.modules.visitor.entity.KeyPersonnel;
import org.exez.modules.visitor.entity.XdPeople;
import org.exez.modules.visitor.mapper.XdPeopleMapper;
import org.exez.modules.visitor.service.IKeyPersonnelService;
import org.exez.modules.visitor.service.IXdPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 西店数据
 * @Author: exez-boot
 * @Date:   2020-08-11
 * @Version: V1.0
 */
@Service
public class XdPeopleServiceImpl extends ServiceImpl<XdPeopleMapper, XdPeople> implements IXdPeopleService {

    @Autowired
    private IKeyPersonnelService keyPersonnelService;


    @Override
    public void updateExternalData() {
        LambdaQueryWrapper<XdPeople> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(XdPeople :: getMemo);
        List<XdPeople> xdPeople1 = baseMapper.selectList(queryWrapper);
        for (XdPeople xdPeople : xdPeople1) {
            KeyPersonnel byId = keyPersonnelService.getById(xdPeople.getIdCard());
            if (null != byId){
                xdPeople.setIsKey(1);
                if (StringUtils.isNotBlank(byId.getZdrylbbj())){
                    xdPeople.setMemo(byId.getZdrylbbj());
                }else {
                    xdPeople.setMemo("");
                }

            } else {
                xdPeople.setIsKey(0);
                xdPeople.setMemo("");
            }
        }
        updateBatchById(xdPeople1);


    }

    @Override
    public IPage<XdPeople> listXdPeople(Page<XdPeople> page, LambdaQueryWrapper<XdPeople> queryWrapper) {
        IPage<XdPeople> pageList = this.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<XdPeople> xdPeopleList() {
        LambdaQueryWrapper<XdPeople> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(XdPeople::getIsKey);
        return baseMapper.selectList(queryWrapper);
    }


    @Override
    public void whitePeople (){
        List<XdPeople> list = xdPeopleList();
        for (int i = 0; i < list.size(); i++) {
            XdPeople xdPeople = list.get(i);
            //调重点人员比对
            KeyPersonnel byId = keyPersonnelService.getById(xdPeople.getIdCard());
            if (null == byId){
                xdPeople.setIsKey(0);
            }else {
                xdPeople.setIsKey(1);
                xdPeople.setMemo(byId.getZdrylbbj());
            }


        }
    }


}
